﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;

namespace Itop.YHPC {
    public class AjaxPage<T> : BasePage {
        protected override void Page_Load(object sender, EventArgs e) {
            base.Page_Load(sender, e);
            string action = Request.QueryString["action"];
            //读取上传的数据流
            System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream, Encoding.UTF8);
            string json = sr.ReadToEnd();
            json = util.U2G(json);//字符串中的中文unicode字符转成中文

            string ret = "";//返回字符串
            if (action == "read") {//查询数据
                ret = read(json);
            } else if (action == "create") {//增加对象
                ret = create(json);
            } else if (action == "update") {//更新对象
                ret = update(json);
            } else if (action == "delete") {//删除对象
                ret = delete(json);
            }
            Response.Write(ret);
            Response.End();
        }
        private string read(string json) {
            //json里可能有分页信息
            IList<T> list = null;
            int total = 0;
            bool success = true;
            string msg="read recoder";
            try {
                list = onRead();
                total = GetTotal();
            } catch(Exception err) {
                success = false;
                msg = err.Message;
                if(Request.Path.Contains("local"))
                    msg += "\r\n" + Request.Url; 
                
            }
            if (list == null) list = new List<T>();
            retModelList<T> retm = new retModelList<T>() { message = msg, success = success, total = GetTotal(), data = list };
            return Newtonsoft.Json.JsonConvert.SerializeObject(retm);
        }

        protected virtual IList<T> onRead() {

            return null;
        }

        protected virtual int GetTotal()
        {
            return 0;
        }
        private string update(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<T> list = null;//
            try {
                list = new List<T>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel<T>>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList<T>>(json).data;
            }
           
            retModelList<T> retm = new retModelList<T>() { message = "Update recoder", data = list };
            try
            {
                retm.success = onUpdate(list);
                if (list.Count>0 &&　list[0] == null)
                    retm.success = onUpdate(json);
             } catch (Exception err) {
                retm.success = false;
                retm.message = err.Message;
            }return Newtonsoft.Json.JsonConvert.SerializeObject(retm);
            //return "{success:true}";
        }

        protected virtual bool onUpdate(string json)
        {

            return true;
        }
        /// <summary>
        /// 更新数据库记录
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        protected virtual bool onUpdate(IList<T> list) {

            return true;
        }
        private string create(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<T> list = null;//
            try {
                list = new List<T>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel<T>>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList<T>>(json).data;
            }
            
            retModelList<T> retm = new retModelList<T>() { data = list };
            try {
                retm.success = onCreate(list);
                if (list.Count > 0 && list[0] == null)
                    retm.success = onCreate(json);
            } catch (Exception err) {
                retm.success = false;
                retm.message = err.Message;
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(retm);
        }

        protected virtual bool onCreate(IList<T> list) {

            return true;
        }

        protected virtual bool onCreate(string json)
        {

            return true;
        }
        private string delete(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<T> list = null;//
            try {
                list = new List<T>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel<T>>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList<T>>(json).data;
            }
            retModel<T> retm = new retModel<T>() {  message="delete record" };
            retm.success = onDelete(list);

            return Newtonsoft.Json.JsonConvert.SerializeObject(retm);
        }

        protected virtual bool onDelete(IList<T> list) {

            return false;
        }
    class retModelList<T> {

            public int total = 0;
            public bool success = true;
            public IList<T> data ;
            public string message = "Created record";
        }
     class retModel<T>
        {
            public int total = 0;
            public bool success = true;
            public T data ;
            public string message = "Created record";
        }
    }
}
